package com.vf.admin.miaodong.controller;

import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppComment;
import com.vf.admin.miaodong.entity.AppCommentReply;
import com.vf.admin.miaodong.entity.AppDynamic;
import com.vf.admin.miaodong.service.AfterCommentReplyService;
import com.vf.admin.miaodong.service.AfterCommentService;
import com.vf.admin.miaodong.service.AfterDynamicService;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.vf.admin.common.Constants.jianshao;
import static com.vf.admin.common.Constants.status_jinyong;
import static com.vf.admin.common.Constants.status_zhengchang;


@Controller
@RequestMapping("/after/dynamic")
public class AfterDynamicController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AfterDynamicService afterDynamicService;
    @Autowired
    AfterCommentService afterCommentService;
    @Autowired
    AfterCommentReplyService afterCommentReplyService;
    @Autowired
    AppUserService afterUserService;
    /**
     * 动态列表
     *
     * @param splitPageBean
     * @param startDate     开始时间
     * @param endDate       结束时间
     * @param nick          用户昵称
     * @param phone         用户手机号
     * @param status        //1正常 2屏蔽(删除)
     * @return
     */

    @RequestMapping("/list")
    public String list(SplitPageBean splitPageBean, String startDate, String endDate, String nick, String phone, Integer status) {
        try {
            Map<String, Object> paramap = new HashMap<>();

            if (StringEx.isNotNull(startDate)) {
                paramap.put("startDate", startDate);
            }

            if (StringEx.isNotNull(endDate)) {
                paramap.put("endDate", endDate);
            }

            if (StringEx.isNotNull(nick)) {
                paramap.put("nick", nick);
            }

            if (StringEx.isNotNull(phone)) {
                paramap.put("phone", phone);
            }

            paramap.put("status", status_zhengchang);
            List<AppDynamic> appDynamicList = afterDynamicService.findList(paramap, splitPageBean);
            request.setAttribute("list", appDynamicList);
            request.setAttribute("splitPage",splitPageBean);
            request.setAttribute("phone",phone);
            request.setAttribute("nick",nick);
            request.setAttribute("endDate",endDate);
            request.setAttribute("startDate",startDate);
            return this.sucess("/web/after/dynamic/list");
        } catch (Exception e) {
            logger.error("动态列表异常", e);
            return this.error("系统繁忙");
        }

    }

    /**
     * 删除动态
     */
    @RequestMapping("/deldongtai")
    @ResponseBody
    public Result deldongtai(Integer id) {

        try {
            AppDynamic dynamic = afterDynamicService.getByid(id);
            afterDynamicService.updateStatus(id, status_jinyong);
            //减少用户动态总量
            afterUserService.dongtaiallUpdate(dynamic.getUserid(),jianshao);
            return this.sucessJSON("删除成功");
        } catch (Exception e) {
            return this.errorJSON("系统繁忙");
        }
    }

    //查看评论列表
    @RequestMapping("/pinglunlist")
    public String pinglunlist(SplitPageBean splitPageBean, String startDate, String endDate, Integer id,String nick, String phone) {
        try {
            Map<String, Object> paramap = new HashMap<>();
            if (StringEx.isNotNull(startDate)) {
                paramap.put("startDate", startDate);
            }

            if (StringEx.isNotNull(endDate)) {
                paramap.put("endDate", endDate);
            }

            if (StringEx.isNotNull(nick)) {
                paramap.put("nick", nick);
            }

            if (StringEx.isNotNull(phone)) {
                paramap.put("phone", phone);
            }

            paramap.put("dynamicid", id);
            paramap.put("status", status_zhengchang);
            List<AppComment> appCommentList = afterCommentService.list(paramap, splitPageBean);
            request.setAttribute("list", appCommentList);
            request.setAttribute("splitPage",splitPageBean);
            request.setAttribute("phone",phone);
            request.setAttribute("nick",nick);
            request.setAttribute("endDate",endDate);
            request.setAttribute("startDate",startDate);
            return this.sucess("/web/after/dynamic/pllist");
        } catch (Exception e) {
            logger.error("动态列表异常", e);
            return this.error("系统繁忙");
        }

    }

    /**
     * 删除动态评论
     */
    @RequestMapping("/deldtpl")
    @ResponseBody
    public Result deldtpl(Integer id, Integer dynamicId) {
        try {
            afterCommentService.updateStatus(id, status_jinyong);

            afterCommentService.pinglunNumUpdate(dynamicId, 2);
            return this.sucessJSON("删除成功");
        } catch (Exception e) {
            return this.errorJSON("系统繁忙");
        }
    }


    //查看评论回复列表
    @RequestMapping("/huifulist")
    public String huifulist(SplitPageBean splitPageBean, String startDate, String endDate, Integer id,String nick, String phone) {
        try {
            Map<String, Object> paramap = new HashMap<>();
            if (StringEx.isNotNull(startDate)) {
                paramap.put("startDate", startDate);
            }

            if (StringEx.isNotNull(endDate)) {
                paramap.put("endDate", endDate);
            }

            if (StringEx.isNotNull(nick)) {
                paramap.put("nick", nick);
            }

            if (StringEx.isNotNull(phone)) {
                paramap.put("phone", phone);
            }

            paramap.put("commentid", id);
            paramap.put("status", status_zhengchang);
            List<AppCommentReply> appCommentReplyList = afterCommentReplyService.list(paramap, splitPageBean);
            request.setAttribute("list", appCommentReplyList);
            request.setAttribute("startDate", startDate);
            request.setAttribute("endDate", endDate);
            request.setAttribute("nick", nick);
            request.setAttribute("phone", phone);
            request.setAttribute("splitPage",splitPageBean);
            return this.sucess("/web/after/dynamic/plhflist");
        } catch (Exception e) {
            logger.error("动态列表异常", e);
            return this.error("系统繁忙");
        }
    }

    /**
     * 删除动态评论回复
     */
    @RequestMapping("/deldthf")
    @ResponseBody
    public Result deldthf(Integer id) {
        try {
            afterCommentReplyService.updateStatus(id, status_jinyong);
            return this.sucessJSON("删除成功");
        } catch (Exception e) {
            return this.errorJSON("系统繁忙");
        }
    }

}
